package com.bk.game.global;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.app.Application;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.OnActionCompleted;
import com.badlogic.gdx.scenes.scene2d.actions.MoveTo;
import com.badlogic.gdx.utils.Disposable;
import com.bk.game.cat.Cat;
import com.bk.game.cat.CatActor;

/**
 * 描述:
 * 
 * @author linqiang(866116)
 * @Since 2012-8-29
 */
public class BkGlobal extends Application implements Disposable {
	public static String TAG = "BkGobal";

	private List<Cat> cats;
	private List<Sound> catSounds;
	private List<TextureAtlas> tas;
	private static BkGlobal instance;
	public static AssetManager manager;
	public static List<CatActor> actors;
	public static List<CatActor> flyActors;
	public static float STEP_X, STEP_Y;
	// public static float scaleX, scaleY;
	public static float factorx, factory;
	public static Game currentGame;
	public static Screen mainScreen;
	public static Screen gameScreen;
	public static int score;
	public static float NORMAL_TIME = 6f;
	public static boolean ISFINISHED = false;
	public List<Integer> ranks;
	private SharedPreferences sp;

	public static Sound btnSound;
	public static Sound buzzerSound;

	public static BkGlobal getInstance() {
		return instance;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		instance = this;
		actors = new LinkedList<CatActor>();
		flyActors = new LinkedList<CatActor>();
		Collections.synchronizedList(actors);
		Collections.synchronizedList(flyActors);
		initRank();
	}

	/**
	 * 描述:加载资源
	 * 
	 * @author linqiang(866116)
	 * @Since 2012-9-5
	 * @param manager
	 */
	public void loadResource(AssetManager manager) {
		cats = new ArrayList<Cat>();
		tas = new ArrayList<TextureAtlas>();
		TextureAtlas ta = manager.get("cat0.pack", TextureAtlas.class);
		loadCat(ta, "cat0");
		TextureAtlas ta1 = manager.get("cat1.pack", TextureAtlas.class);
		loadCat(ta1, "cat1");
		TextureAtlas ta2 = manager.get("cat2.pack", TextureAtlas.class);
		loadCat(ta2, "cat2");
		TextureAtlas ta3 = manager.get("cat3.pack", TextureAtlas.class);
		loadCat(ta3, "cat3");
		catSounds = new ArrayList<Sound>();
		Sound sound1 = manager.get("neko1.mp3", Sound.class);
		catSounds.add(sound1);
		Sound sound2 = manager.get("neko2.mp3", Sound.class);
		catSounds.add(sound2);
		Sound sound3 = manager.get("neko3.mp3", Sound.class);
		catSounds.add(sound3);
		Sound sound4 = manager.get("neko4.mp3", Sound.class);
		catSounds.add(sound4);
		Sound sound5 = manager.get("neko5.mp3", Sound.class);
		catSounds.add(sound5);
	}

	private void loadCat(TextureAtlas ta, String name) {
		TextureRegion[] leftRegion = new TextureRegion[4];
		TextureRegion[] rightRegion = new TextureRegion[4];
		Cat cat = new Cat();
		cat.index = cats.size();
		cat.name = name;
		for (int i = 0; i < 4; i++) {
			leftRegion[i] = ta.findRegion("left" + (i + 1));
			rightRegion[i] = ta.findRegion("rigth" + (i + 1));
		}
		cat.catLeft = leftRegion;
		cat.catRight = rightRegion;
		cat.bangleft = ta.findRegion("bangleft");
		cat.bangright = ta.findRegion("bangright");
		cats.add(cat);
		tas.add(ta);
	}

	public Cat getRandom() {
		int index = (int) (Math.random() * 4);
		return cats.get(index);
	}

	public Sound getRandomSound(int index) {
		// int index = (int) (Math.random() * 5);
		return catSounds.get(index);
	}

	/**
	 * 描述:
	 * 
	 * @author linqiang(866116)
	 * @Since 2012-9-6
	 * @param actor
	 */
	public void flayAction(final CatActor actor) {
		MoveTo move = MoveTo.$(Gdx.graphics.getWidth() / 2,
				Gdx.graphics.getHeight(), 2f);
		move.setCompletionListener(new OnActionCompleted() {
			@Override
			public void completed(Action arg0) {
				actor.remove();
			}
		});
		actor.action(move);
	}

	private void initRank() {
		ranks = new LinkedList<Integer>();
		sp = getSharedPreferences("com.bk.game.rank", MODE_PRIVATE);
		for (int i = 0; i < 5; i++) {
			ranks.add(sp.getInt("rank" + i, 0));
		}
	}

	/**
	 * 描述:保存新纪录
	 * 
	 * @author linqiang(866116)
	 * @Since 2012-9-10
	 */
	public void rank() {
		Editor sharedata = sp.edit();
		for (int i = 0; i < 5; i++) {
			sharedata.putInt("rank" + i, ranks.get(i));
		}
		sharedata.commit();
	}

	public List<Integer> getRanks() {
		return ranks;
	}

	@Override
	public void dispose() {
		if (tas != null) {
			for (TextureAtlas ta : tas) {
				ta.dispose();
			}
		}

		if (catSounds != null) {
			for (Sound s : catSounds) {
				s.dispose();
			}
			catSounds.clear();
		}

		if (cats != null) {
			cats.clear();
		}

		if (btnSound != null)
			btnSound.dispose();

		if (buzzerSound != null)
			buzzerSound.dispose();

	}
}
